﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace OrderApp2
{
    public  class OrderService
    {
        //商品list
        public List<OrderItem> items = new List<OrderItem>();
        //订单list
        public static  List<Order> orders = new List<Order>();
        //订单号排序
        public static void sortBy_orderID()
        {
            orders.Sort((o1, o2) => o1.orderID - o2.orderID);
        }
        //增添订单
        public static void order_toAdd(Order order)
        {
            orders.Add(order);
        }
        //删除订单
        public static  bool order_toDel(int order_ID)
        {
            foreach( Order order0 in orders)
            {
                if (order0.orderID==order_ID)
                {
                    orders.Remove(order0);
                    return true;
                }  
            } return false;
        }

        //修改订单信息
        public static bool order_toAlter(int order_ID,int no_alter,ArrayList alter_content)
        {
           foreach(Order order in orders)
            {
                if (order.orderID == order_ID)
                {
                    foreach (OrderItem orderItem in order.orderItems)
                  {
                        switch (no_alter)
                      {
                        case 1:
                            order.cusID = (int)alter_content[0];
                            break;
                        case 2:
                            order.shippingAddress = (string)alter_content[0];
                            break;
                        case 3:
                            orderItem.itemName = (string)alter_content[0];
                            break;
                        case 4:
                           orderItem.itemPrice = (double)alter_content[0];
                           break;
                        case 5:
                           orderItem.itemNum = (int)alter_content[0];
                           break;
                        }
                  }
                    return true;
                }
                return true;
                     
            }

          
            return false;
        }


       //按订单号查询订单
        public static List<Order> SearchBy_orderID(int orderID)
        {
            var order_s = from od in orders
                          where od.orderID == orderID
                          orderby od.orderPrice
                          select od;
            if (order_s != null)
                return order_s.ToList();
            else
                return null;

        }


        //按顾客会员号查询订单
        public static List<Order> SearchBy_cusID(int cusID )
        {
            var order_s = from od in orders
                          where od.cusID == cusID
                          orderby od.orderPrice
                          select od;
            if (order_s != null)
                return order_s.ToList();

            else
                return null;

        }
        //按商品名称查询
        public static List<Order> SearchBy_itemName(string itemName)
        {
            List<Order> searchByitems = new List<Order>();
            foreach(Order order in orders)
               foreach(OrderItem orderItem in order.orderItems)
                {
                    if (orderItem.itemName == itemName)
                        searchByitems.Add(order);
                }
            return searchByitems;
        }

        public class OrderServiceException : ApplicationException
        {
            public OrderServiceException(string message) : base(message)
            {
            }
        }
    }
}
